﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BEGDC.CC.BL.Common.Query;
using BEGDC.CC.Entity;

namespace ASB.CC.Web.UserControls
{
    public partial class RequesterInfoControl : System.Web.UI.UserControl
    {
        #region Variables

        private bool _isReadOnly = false;
        private BEGDC.CC.Entity.Application _Application = null;
        private Employee employee = null;

        #endregion

        #region Set Variables

        public bool isReadOnly
        {
            set
            {
                _isReadOnly = value;
            }
            get
            {
                return _isReadOnly;
            }
        }

        public Employee Employee
        {
            set
            {
                employee = value;
            }
            get
            {
                return employee;
            }
        }

        public string NameEN
        {
            get
            {
                // Get Requester
                if (this._Application != null && this._Application.Id > 0 && this._Application.RequesterNo != null)
                {
                    return this._Application.RequesterNo.NameEN;
                }
                else
                {
                    return ((BEGDC.CC.BL.Workflow.CCIdentity)this.Page.User.Identity).UserInfo.NameEN;
                }
            }
        }

        public string Mobile
        {
            get
            {
                if (!string.IsNullOrEmpty(this.edt_Mobile.Text))
                {
                    return this.edt_Mobile.Text;
                }
                else
                {
                    return this.vw_Mobile.Text;
                }
            }
        }

        public string OfficePhone
        {
            get
            {
                if (!string.IsNullOrEmpty(this.edt_Telephone.Text))
                {
                    return this.edt_Telephone.Text;
                }
                else
                {
                    return this.vw_Telephone.Text;
                }
            }
        }

        #endregion

        /// <summary>
        /// Get Applicant By QueryString
        /// </summary>
        private void InitApplication()
        {
            if (Request.QueryString["AppID"] + string.Empty != string.Empty)
            {
                int applicantID = Convert.ToInt32(Request.QueryString["AppID"]);
                if (applicantID > 0)
                {
                    this._Application = (BEGDC.CC.Entity.Application)Session[applicantID.ToString()];
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            //this.edt_Mobile.TextChanged += new EventHandler(edt_Mobile_TextChanged);
            //this.edt_Telephone.TextChanged += new EventHandler(edt_Telephone_TextChanged);
            this.Load += new System.EventHandler(Page_Load);
            base.OnInit(e);
        }

        //void edt_Telephone_TextChanged(object sender, EventArgs e)
        //{
        //    employee.OfficePhone = this.edt_Telephone.Text;
        //}

        //void edt_Mobile_TextChanged(object sender, EventArgs e)
        //{
        //    employee.Mobile = this.edt_Mobile.Text;
        //}

        protected void Page_Load(object sender, EventArgs e)
        {
            InitApplication();

            // Get Requester
            if (this._Application != null && this._Application.Id > 0 && this._Application.RequesterNo != null)
            {
                employee = this._Application.RequesterNo;
                employee.OfficePhone = this._Application.RequesterOfficeTel;
                employee.Mobile = this._Application.RequesterMobile;
                //employee.Mobile = string.IsNullOrEmpty(this.edt_Mobile.Text) ? this.vw_Mobile.Text : this.edt_Mobile.Text;
            }
            else
            {
                employee = ((BEGDC.CC.BL.Workflow.CCIdentity)this.Page.User.Identity).UserInfo;
            }

            if (!this.IsPostBack)
            {
                if (employee != null)
                {
                    // Set Page Value
                    this.lblApplicantRequesterName.Text = string.Format("{0} ({1})<br/> &nbsp; {2}", employee.NameEN, employee.Id, employee.NameChinese);
                    this.vw_Title.Text = employee.Title;

                    if (_isReadOnly)
                    {
                        this.vw_Mobile.Visible = true;
                        this.vw_Telephone.Visible = true;
                        this.edt_Telephone.Visible = false;
                        this.edt_Mobile.Visible = false;
                        this.vw_Mobile.Text = employee.Mobile;
                        this.vw_Telephone.Text = employee.OfficePhone;
                    }
                    else
                    {
                        this.edt_Telephone.Text = employee.OfficePhone;
                        this.edt_Mobile.Text = employee.Mobile;
                    }
                }
                else
                {
                    Page.RegisterClientScriptBlock("", "<script>alert('The Requester is not exist!');</script>");
                }
            }
        }
    }
}